package demo2;


import java.util.Arrays;
import java.util.Comparator;

public class Test2 {
     public static void bubbleSort(Comparable[] comparables){
         for (int i = 0; i < comparables.length - 1; i++) {
             for (int j = 0; j < comparables.length - 1 - i; j++) {
                 if(comparables[j].compareTo(comparables[j+1]) > 0){
                     Comparable tmp =  comparables[j] ;
                     comparables[j] = comparables[j+1] ;
                     comparables[j+1] = tmp ;
                 }
             }
         }


     }

    public static void main(String[] args) {
        Student student1 =new Student("张三" ,16);
        Student student2 =new Student("李四" ,12);
//        AgeComparator ageComparator = new AgeComparator() ;
//        System.out.println(ageComparator.compare(student1, student2));
//        NameComparator nameComparator = new NameComparator() ;
//        System.out.println(nameComparator.compare(student1, student2));
        System.out.println(student1.compareTo(student2));

    }
//    public static void main1(String[] args) {
//      Student[] students = {new Student("张三" ,16),new Student("李四" ,12),
//              new Student("王五" ,10),new Student("赵六" ,14)} ;
////        Arrays.sort(students);
//        bubbleSort(students);
//        System.out.println(Arrays.toString(students));
//
//    }
}
class AgeComparator implements Comparator <Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name) ;
    }
}

class Student  implements Comparable<Student>  {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age  - o.age ;
    }
}

//class Student implements Comparable<Student>{
//    String name ;
//    int age ;
//
//    public Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public int compareTo(Student o) {
//       return this.age - o.age ;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//}
